package com.tools.acceleratecore.xlib.util;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.text.TextUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class UtilHardware {
    private static final String VALUE_STRING_PROC_STAT_FILE = "/proc/stat";
    private static final String VALUE_STRING_MEMORY_FILE = "/proc/meminfo";

    public static long getCpuTimeTotal() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return 0L;
        }
        String strCpuStat = UtilIO.readFirstLineInFile("/proc/stat");
        if (TextUtils.isEmpty(strCpuStat)) {
            return 0L;
        }
        String[] arraystr = strCpuStat.split("\\s+");
        if ((arraystr == null) || (arraystr.length < 8)) {
            return 0L;
        }
        long lCPUTimeTotal = 0L;
        for (int nIndex = 1; nIndex < 8; nIndex++) {
            lCPUTimeTotal += Long.parseLong(arraystr[nIndex]);
        }
        return lCPUTimeTotal;
    }

    public static long getCpuIdleTime() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return 0L;
        }

        String strCpuStat =
                UtilIO.readFirstLineInFile("/proc/stat");
        if (TextUtils.isEmpty(strCpuStat)) {
            return 0L;
        }
        String[] arraystr = strCpuStat.split("\\s+");
        if ((arraystr == null) || (arraystr.length < 8)) {
            return 0L;
        }
        return Long.parseLong(arraystr[4]);
    }

    public static double getCpuUsageTotal() {
        long lCpuTimeTotal1 = getCpuTimeTotal();
        long lCpuIdleTime1 = getCpuIdleTime();
        try {
            Thread.sleep(500L);
        } catch (Exception e) {
            e.printStackTrace();
        }

        long lCpuTimeTotal2 = getCpuTimeTotal();
        long lCpuIdleTime2 = getCpuIdleTime();
        if (lCpuTimeTotal2 - lCpuTimeTotal1 == 0L) {
            return 0.0D;
        }
        double dCpuUsageTotal = (lCpuTimeTotal2 - lCpuTimeTotal1 - (lCpuIdleTime2 - lCpuIdleTime1)) / (
                lCpuTimeTotal2 - lCpuTimeTotal1);
        return dCpuUsageTotal;
    }

    public static double getCpuUsageTotalWithoutOwnPID() {
        long lCpuTimeTotal1 = getCpuTimeTotal();
        long lCpuIdleTime1 = getCpuIdleTime();
        long lCpuTimeOwnPID1 = getCpuTimeByPID(UtilProcess.getMyPID());
        try {
            Thread.sleep(500L);
        } catch (Exception e) {
            e.printStackTrace();
        }

        long lCpuTimeTotal2 = getCpuTimeTotal();
        long lCpuIdleTime2 = getCpuIdleTime();
        long lCpuTimeOwnPID2 = getCpuTimeByPID(UtilProcess.getMyPID());
        if (lCpuTimeTotal2 - lCpuTimeTotal1 == 0L) {
            return 0.0D;
        }
        double dCpuUsageTotal = (lCpuTimeTotal2 - lCpuTimeTotal1 - (lCpuIdleTime2 - lCpuIdleTime1) - (
                lCpuTimeOwnPID2 - lCpuTimeOwnPID1)) / (
                lCpuTimeTotal2 - lCpuTimeTotal1);
        return dCpuUsageTotal;
    }

    public static long getCpuTimeByPID(int nPID) {
        String strStat = UtilIO.readFirstLineInFile("/proc/" + nPID + "/stat");
        if (TextUtils.isEmpty(strStat)) {
            return 0L;
        }
        String[] arraystr = strStat.split("\\s+");
        if ((arraystr == null) || (arraystr.length < 17)) {
            return 0L;
        }
        long lCpuTimeRun = Long.parseLong(arraystr[13]) +
                Long.parseLong(arraystr[14]) + Long.parseLong(arraystr[15]) +
                Long.parseLong(arraystr[16]);

        return lCpuTimeRun;
    }

    public static List<Integer> getPIDList() {
        List listPID = new ArrayList();
        File fileProc = new File("/proc/");
        String[] arraystrFile = fileProc.list();
        if (arraystrFile == null) {
            return null;
        }
        for (int nIndex = 0; nIndex < arraystrFile.length; nIndex++) {
            if (arraystrFile[nIndex].matches("[0-9]+")) {
                listPID.add(Integer.valueOf(Integer.parseInt(arraystrFile[nIndex])));
            }
        }

        return listPID;
    }

    public static long getMemoryTotalSize() {
        String str = UtilIO.readFirstLineInFile("/proc/meminfo");
        if (TextUtils.isEmpty(str)) {
            return 0L;
        }
        int begin = str.indexOf(':');
        int end = str.indexOf('k');
        str = str.substring(begin + 1, end).trim();
        return Long.parseLong(str);
    }

    public static double getBatteryTemperature(Context context) {
        if (context == null) {
            return 0.0D;
        }
        int nTemperature = 340;
        Intent bat = context
                .registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
        if (bat != null) {
            nTemperature = bat.getIntExtra("temperature", 340);
        }
        if (nTemperature == 0) {
            nTemperature = 340;
        }
        return nTemperature / 10.0D;
    }
}
